package com.shelpe.services.sinbad.service.simbaproxy;

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.sinbad.service.simbaproxy.RankClient.RankClientFallback;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

@FeignClient(name = "${feign.simba.name}", url = "${feign.simba.url}", fallback = RankClientFallback.class)
public interface RankClient {
	public final static Long[] TEMPLATE = new Long[0];

	@RequestMapping(path = "/simba/taobao.simba.keywords.realtime.ranking.batch.get", method = RequestMethod.GET)
	KeywordRealtimeRankItemResponse findRealtimeRanks(@RequestParam("nick") String nick,
			@RequestParam("ad_group_id") long adgroupID, @RequestParam("bidword_ids") List<Long> keywordIDs);

	@Data
	@EqualsAndHashCode(callSuper = true)
	public static class KeywordRealtimeRankItemResponse extends SimbaProxyBaseDto {
		@JsonProperty("simba_keywords_realtime_ranking_batch_get_response")
		private KeywordRealtimeRankItemsResult res;
	}

	@Data
	public static class KeywordRealtimeRankItemsResult {
		@JsonProperty("result")
		private KeywordRealtimeRankItemsRes result;
	}

	@Data
	public static class KeywordRealtimeRankItemsRes {
		@JsonProperty("realtime_rank_list")
		private KeywordRealtimeRankItems dataList;
	}

	@Data
	public static class KeywordRealtimeRankItems {
		@JsonProperty("result")
		private List<KeywordRealtimeRankItem> items;
	}

	@Data
	public static class KeywordRealtimeRankItem {
		@JsonProperty("bidwordid")
		private long keywordID;
		@JsonProperty("state")
		private int state;
		@JsonProperty("pc_rank")
		private int pcRank;// PC的排名:(0:首页左侧位置;1:首页右侧第1;2:首页右侧第2,3:首页右侧第3,4:首页非前三,5:第2页,6:第3页,7:第4页,8:第5页,9:5页以后)
		@JsonProperty("mobile_rank")
		private int moibleRank;// 移动的排名(0:移动首条,1:移动前三,3:移动4~6条,6:移动7~10条,10:移动11~15条,11:移动16~20条,12:20条以后)
	}

	@Service
	@Slf4j
	public static class RankClientFallback implements RankClient {
		@Override
		public KeywordRealtimeRankItemResponse findRealtimeRanks(String nick, long adgroupID, List<Long> keywordIDs) {
			log.error("fallback findRealtimeRanks: {}, {}", nick, adgroupID, keywordIDs);
			SimbaProxyBaseDto.Error error = new SimbaProxyBaseDto.Error(500, "fallback");

			KeywordRealtimeRankItemResponse res = new KeywordRealtimeRankItemResponse();
			res.setError(error);
			return res;
		}
	}
}
